package com.icss.company.service.impl;

import com.icss.company.dao.DeptMapper;
import com.icss.company.pojo.Dept;
import com.icss.company.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Collections;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    @Transactional
    public boolean add(Dept dept) {
        try {
            // 获取最大部门编号并加1
            Integer maxDeptno = deptMapper.getMaxDeptno();
            dept.setDeptno(maxDeptno == null ? 10 : maxDeptno + 10);
            return deptMapper.add(dept) > 0;
        } catch (Exception e) {

            return false;
        }
    }
    @Override
    @Transactional(readOnly = true)
    public List<Dept> list(String dname) {
        return deptMapper.list(dname);
    }

    @Override
    public boolean del(Integer deptno) {
        return deptMapper.del(deptno) > 0;
    }

//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
//    public boolean add(Dept dept) {
//        return deptMapper.add(dept) > 0;
//    }

    @Override
    public Dept get(Integer deptno) {
        return deptMapper.get(deptno);
    }

    @Override
    public boolean update(Dept dept) {
        return deptMapper.update(dept) > 0;
    }
}
